<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    /* 
    Promise类型对象有三个状态：pending进行中、resolved成功、rejected失败。

    */
    let p = new Promise((resolve, reject) => {
        // 此处为异步代码，待异步代码执行完后，调用resolve或reject来更新Promise对象的状态
        // Promise状态被更新的同时，.then方法被回调 
        setTimeout(() => {
            console.log('异步代码执行...')
            let data = '从数据库中查询的数据'
            let num = Math.floor(Math.random() * 10 + 1) // 1 ~ 10
            if(num%2 === 0) {
                resolve(data)
            } else {
                reject(data)
            }
        }, 1000)
    })

    p.then((data) => {
        console.log('resolved:', data)
    }).catch((data) => {
        console.log('rejected:', data)
    })

</script>
</html>